﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnalysisSystemLog.Define;

namespace AnalysisSystemLog
{
    public partial class MainForm : Form
    {
        TimeSpan tsTotal = new TimeSpan();
        bool bIsFirstStart = true;
        // 文件保存路径
        private string strFilePath = Directory.GetCurrentDirectory() + "\\EventLogCSharpRecords.txt";
        //记录listview中的items
        private List<LogRecord> listStructOfListView = new List<LogRecord>();
        //记录文件中读取的items
        private List<LogRecord> listStructOfFile = new List<LogRecord>();
        //记录最后要写入文件的items
        private List<LogRecord> listStructOfWriteToFile = new List<LogRecord>();
        //目前焦点所处位置
        private int JumpIndex = 0; 

        // 获取服务器工作时间
        public string WorkTime
        {
            get
            {
                TimeSpan ts = new TimeSpan(0, 0, 0, 0, Environment.TickCount);
                return (ts.Days == 0 ? "" : (ts.Days.ToString() + "天"))
                    + (ts.Hours == 0 ? "" : (ts.Hours.ToString() + "小时"))
                    + ts.Minutes.ToString("00") + "分钟" + ts.Seconds.ToString("00") + "秒";
            }
        }

        /**
         * \brief 显示日志列表信息
         */
        private void ShowEventLogToListView()
        {
            listViewShow.Items.Clear();
            tsTotal = new TimeSpan(0, 0, 0, 0, Environment.TickCount);
            labelTimeContinue.Text = tsTotal.Days.ToString() + "天" + tsTotal.Hours.ToString() + "小时" + tsTotal.Minutes.ToString() + "分钟" + tsTotal.Seconds.ToString() + "秒";

            if (bIsFirstStart)
            {
                SystemEventLog eve = new SystemEventLog();
                eve.GetRecords(new int[] { (int)SystemEventCode.Start, (int)SystemEventCode.Shutdown });
                ShowDataListToListView(eve.EventLogDataList);
            }
        }

        /**
         * \brief 将内存中的记录显示到listView上
         *
         * \param list 数据列表
         */
        private void ShowDataListToListView(List<LogRecord> list)
        {
            listViewShow.Items.Clear();
            int iOnOffTimes = 0;
            //序号
            int iSum = 0;
            DateTime dt = new DateTime();
            tsTotal = new TimeSpan(0, 0, 0, 0, Environment.TickCount);
            bool bIsON = false;
            foreach (var v in list)
            {
                if (v.InnerID.Equals("开机"))
                {
                    listViewShow.Items.Add(new ListViewItem(LogRecord.ConvertRecordToStringArray(v)));
                    if (v.Context.Equals("非正常关机"))
                    {
                        listViewShow.Items[listViewShow.Items.Count - 1].BackColor = System.Drawing.Color.LightGray;
                    }

                    listViewShow.Items[listViewShow.Items.Count - 1].ForeColor = System.Drawing.Color.Green;

                    iSum++;
                    listViewShow.Items[listViewShow.Items.Count - 1].SubItems[5].Text = iSum.ToString();

                    dt = LogRecord.ConvertStringToDateTime(v.GeneratedTime);
                    bIsON = true;
                }
                else if (v.InnerID.Equals("关机"))
                {
                    listViewShow.Items.Add(new ListViewItem(LogRecord.ConvertRecordToStringArray(v)));
                    if (v.Context.Equals("非正常开机"))
                    {
                        listViewShow.Items[listViewShow.Items.Count - 1].BackColor = System.Drawing.Color.LightGray;
                    }

                    listViewShow.Items[listViewShow.Items.Count - 1].ForeColor = System.Drawing.Color.Red;

                    iSum++;
                    listViewShow.Items[listViewShow.Items.Count - 1].SubItems[5].Text = iSum.ToString();

                    if (bIsON)
                    {
                        TimeSpan ts = LogRecord.ConvertStringToDateTime(v.GeneratedTime).Subtract(dt);
                        tsTotal += ts;
                        iOnOffTimes++;
                    }
                }
            }
            labelOnOffTimes.Text = "开关机次数:" + iOnOffTimes.ToString();
            if (listViewShow.Items.Count > 0)
            {
                listViewShow.Items[listViewShow.Items.Count - 1].EnsureVisible();
            }
        }

        /**
         * \brief 初始状态将事件日志中的显示到listView，然后会根据这个表来合并文件中的list
         *
         * \param list 数据列表
         */
        private void ShowDataListToListView(List<SystemEventLog.EventLogData> list)
        {
            listViewShow.Items.Clear();
            int iOnOffTimes = 0;
            bool bIsLastStart = false;
            bool bIsLastShutdown = true;
            DateTime dt = new DateTime();
            foreach (var v in list)
            {
                if (v.EventID == (int)SystemEventCode.Start)
                {
                    //MessageBox.Show("开机：" + v.timeGenerated.ToString());

                    if (bIsLastShutdown && !bIsLastStart)
                    {
                        listViewShow.Items.Add(new ListViewItem(new string[] { listViewShow.Items.Count.ToString(), "开机", v.TimeGenerated.ToString("yyyy-M-d HH:mm:ss"), System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(v.TimeGenerated.DayOfWeek), "", "" }));
                        listViewShow.Items[listViewShow.Items.Count - 1].ForeColor = System.Drawing.Color.Green;
                    }
                    else
                    {
                        listViewShow.Items.Add(new ListViewItem(new string[] { listViewShow.Items.Count.ToString(), "开机", v.TimeGenerated.ToString("yyyy-M-d HH:mm:ss"), System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(v.TimeGenerated.DayOfWeek), "", "" }));
                        listViewShow.Items[listViewShow.Items.Count - 1].ForeColor = System.Drawing.Color.Green;
                        if (listViewShow.Items.Count - 2 >= 0)
                        {
                            listViewShow.Items[listViewShow.Items.Count - 2].BackColor = System.Drawing.Color.LightGray;
                            listViewShow.Items[listViewShow.Items.Count - 2].SubItems[4].Text = "非正常关机";
                        }
                    }
                    bIsLastStart = true;
                    bIsLastShutdown = false;
                    dt = v.TimeGenerated;
                }
                else
                {
                    if (bIsLastStart && !bIsLastShutdown)
                    {
                        TimeSpan ts = v.TimeGenerated.Subtract(dt);
                        listViewShow.Items.Add(new ListViewItem(new string[] { listViewShow.Items.Count.ToString(), v.TimeGenerated.ToString("yyyy-M-d HH:mm:ss"), System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(v.TimeGenerated.DayOfWeek), ts.ToString(), "", "" }));
                        listViewShow.Items[listViewShow.Items.Count - 1].ForeColor = System.Drawing.Color.Red;
                        iOnOffTimes++;

                        tsTotal += ts;
                    }
                    else
                    {
                        listViewShow.Items.Add(new ListViewItem(new string[] { listViewShow.Items.Count.ToString(), v.TimeGenerated.ToString("yyyy-M-d HH:mm:ss"), System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(v.TimeGenerated.DayOfWeek), "", "", "" }));
                        listViewShow.Items[listViewShow.Items.Count - 1].ForeColor = System.Drawing.Color.Red;
                        if (listViewShow.Items.Count - 2 >= 0)
                        {
                            listViewShow.Items[listViewShow.Items.Count - 2].BackColor = System.Drawing.Color.LightBlue;
                            listViewShow.Items[listViewShow.Items.Count - 2].SubItems[4].Text = "非正常开机";
                        }
                    }
                    bIsLastStart = false;
                    bIsLastShutdown = true;

                }
            }

            labelOnOffTimes.Text = "开关机次数:" + iOnOffTimes.ToString();
            if (listViewShow.Items.Count > 0)
            {
                listViewShow.Items[listViewShow.Items.Count - 1].EnsureVisible();
            }

        }
    }
}
